<template>
  <div class="analysis-card">
    <h3 class="analysis-title">流量分析</h3>

    <div class="flow-grid">
      <!-- 短视频数据 -->
      <div class="flow-section">
        <h4 class="section-subtitle">短视频</h4>
        <div class="metrics-list">
          <div class="metric-item">
            <div>
              <span class="item-label">成交金额</span>
              <span class="amount">¥{{ getData('视频成交-成交金额') }}</span>
            </div>
            <div class="item-values">
              <div class="comparison-data">
                <span :class="getData('视频成交-成交金额-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('视频成交-成交金额-较上周期变化') }}
                </span>
                <span class="previous-value">上期: ¥{{ getPreviousValues('视频成交-成交金额', '视频成交-成交金额-较上周期变化', '视频成交-成交金额-上周期')
                }}</span>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>
              <span class="item-label">播放次数</span>
              <span class="amount">{{ getData('视频成交-播放次数') }}</span>
            </div>
            <div class="item-values">
              <div class="comparison-data">
                <span :class="getData('视频成交-播放次数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('视频成交-播放次数-较上周期变化') }}
                </span>
                <span class="previous-value">上期: {{ getPreviousValues('视频成交-播放次数', '视频成交-播放次数-较上周期变化', '视频成交-播放次数-上周期')
                }}</span>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>
              <span class="item-label">新发布视频数</span>
              <span class="amount">{{ getData('视频成交-新发布视频') }}条</span>
            </div>
            <div class="item-values">
              <div class="comparison-data">
                <span :class="getData('视频成交-新发布视频-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('视频成交-新发布视频-较上周期变化') }}
                </span>
                <span class="previous-value">上期: {{ getPreviousValues('视频成交-新发布视频', '视频成交-新发布视频-较上周期变化',
                  '视频成交-新发布视频-上周期') }}条</span>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>
              <span class="item-label">官号新增粉丝数</span>
              <span class="amount">{{ getData('视频成交-官号新增粉丝数') }}人</span>
            </div>
            <div class="item-values">
              <div class="comparison-data">
                <span :class="getData('视频成交-官号新增粉丝数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('视频成交-官号新增粉丝数-较上周期变化') }}
                </span>
                <span class="previous-value">上期: {{ getPreviousValues('视频成交-官号新增粉丝数', '视频成交-官号新增粉丝数-较上周期变化',
                  '视频成交-官号新增粉丝数-上周期') }}人</span>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 直播数据 -->
      <div class="flow-section">
        <h4 class="section-subtitle">直播</h4>
        <div class="metrics-list">
          <div class="metric-item">
            <div>
              <span class="item-label">成交金额</span>
              <span class="amount">¥{{ getData('直播间基础数据-直播间成交金额') }}</span>
            </div>
            <div class="item-values">
              <div class="comparison-data">
                <span :class="getData('直播间基础数据-直播间成交金额-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('直播间基础数据-直播间成交金额-较上周期变化') }}
                </span>
                <span class="previous-value">上期: ¥{{ getPreviousValues('直播间基础数据-直播间成交金额', '直播间基础数据-直播间成交金额-较上周期变化',
                  '直播间基础数据-直播间成交金额-上周期') }}</span>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>
              <span class="item-label">观看人数</span>
              <span class="amount">{{ getData('直播间基础数据-直播间观看人数') }}人</span>
            </div>
            <div class="item-values">
              <div class="comparison-data">
                <span :class="getData('直播间基础数据-直播间观看人数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('直播间基础数据-直播间观看人数-较上周期变化') }}
                </span>
                <span class="previous-value">上期: {{ getPreviousValues('直播间基础数据-直播间观看人数', '直播间基础数据-直播间观看人数-较上周期变化',
                  '直播间基础数据-直播间观看人数-上周期') }}人</span>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>
              <span class="item-label">成交券数</span>
              <span class="amount">{{ getData('直播间基础数据-直播间成交券数') }}张</span>
            </div>
            <div class="item-values">
              <div class="comparison-data">
                <span :class="getData('直播间基础数据-直播间成交券数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('直播间基础数据-直播间成交券数-较上周期变化') }}
                </span>
                <span class="previous-value">上期: {{ getPreviousValues('直播间基础数据-直播间成交券数', '直播间基础数据-直播间成交券数-较上周期变化',
                  '直播间基础数据-直播间成交券数-上周期') }}张</span>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>
              <span class="item-label">成交人数</span>
              <span class="amount">{{ getData('直播间基础数据-直播间成交人数') }}人</span>
            </div>
            <div class="item-values">
              <div class="comparison-data">
                <span :class="getData('直播间基础数据-直播间成交人数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('直播间基础数据-直播间成交人数-较上周期变化') }}
                </span>
                <span class="previous-value">上期: {{ getPreviousValues('直播间基础数据-直播间成交人数', '直播间基础数据-直播间成交人数-较上周期变化')
                  }}人</span>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>

              <span class="item-label">直播间转化率</span>
              <span class="amount">{{ getData('直播间基础数据-直播间成交转化率') }}</span>
            </div>
            <div class="item-values">
              <div class="comparison-data">
                <span :class="getData('直播间基础数据-直播间成交转化率-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('直播间基础数据-直播间成交转化率-较上周期变化') }}
                </span>
                <span class="previous-value">上期: {{ getPreviousValues('直播间基础数据-直播间成交转化率', '直播间基础数据-直播间成交转化率-较上周期变化')
                  }}</span>
              </div>
            </div>
          </div>

          <div class="metric-item">
            <div>

              <span class="item-label">直播时长</span>
              <span class="amount">{{ getData('直播间基础数据-直播时长') }}</span>
            </div>
            <div class="item-values">
              <div class="comparison-data">
                <span :class="getData('直播间基础数据-直播时长-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                  {{ getData('直播间基础数据-直播时长-较上周期变化') }}
                </span>
                <span class="previous-value">上期: {{ getPreviousValues('直播间基础数据-直播时长', '直播间基础数据-直播时长-较上周期变化') }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getPreviousValue } from '../../../utils'
interface ReportItem {
  name: string
  value: string
}

interface ReportSection {
  title: string
  items: ReportItem[]
}

interface Props {
  sectionData: ReportSection
}

const props = defineProps<Props>()
const getPreviousValues = (currentKey: string, changeKey: string,): string => {

  const currentValue = getData(currentKey)
  const changeValue = getData(changeKey)
  return getPreviousValue(currentValue, changeValue)
}
const getData = (key: string): string => {
  const item = props.sectionData.items.find(item => item.name === key)
  return item?.value || '-'
}

</script>

<style scoped>
.analysis-card {
  margin-bottom: 25px;
  padding: 25px;
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.analysis-title {
  font-size: 20px;
  color: #2d3748;
  font-weight: 600;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 2px solid #e2e8f0;
}

.flow-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 24px;
}

.flow-section {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 20px;
}

.section-subtitle {
  font-size: 16px;
  color: #4a5568;
  margin-bottom: 16px;
  padding-left: 10px;
  border-left: 3px solid #4a5568;
}

.metrics-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.metric-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  background: #ffffff;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.metric-item:hover {
  background: #f0f4f8;
}

.item-label {
  font-size: 14px;
  color: #4a5568;
  display: block;
}

.item-values {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.comparison-data {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.amount {
  font-size: 16px;
  font-weight: bold;
  color: #2d3748;
}

.previous-value {
  font-size: 11px;
  color: #718096;
}

.rate-up,
.rate-down {
  font-size: 13px;
  padding: 2px 6px;
  border-radius: 4px;
}

.rate-up {
  color: #22c55e;
  background: rgba(34, 197, 94, 0.1);
}

.rate-down {
  color: #ef4444;
  background: rgba(239, 68, 68, 0.1);
}

@media (max-width: 768px) {
  .flow-grid {
    grid-template-columns: 1fr;
  }

  .item-values {
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
  }
}
</style>